package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsGoods;
import com.qf.util.AliyunOSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/17 9:28
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private DtsGoodsMapper goodsMapper;

    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //bucket桶空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public int countGoods() {
        QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        Integer count = goodsMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public IPage<DtsGoods> selectGoodsPage(String goods_sn, String name, Integer page, Integer limit, String sort, String order) {

        //1、创建分页对象
        //参数1：当前页，参数2：每页显示多少条数据
        IPage<DtsGoods> iPage = new Page<>(page , limit);

        //2、封装查询对象
        QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        //判断查询条件是否为空,不为空拼接查询条件
        if (!StringUtils.isEmpty(goods_sn)){
            queryWrapper.eq("key", goods_sn);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        //判断排序字段不为空
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //判断是否是升序排序，并传入排序字段
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }
        }

        //3、查询并返回数据
        IPage<DtsGoods> resultPage = goodsMapper.goodList(iPage, queryWrapper);

        return resultPage;
    }

    @Override
    public int update(DtsGoods goods) {
        //设置修改时间
        goods.setUpdateTime(new Date());
        int count = goodsMapper.updateById(goods);
        return count;
    }

    @Override
    public void deleteByKey(String key) {
    //1. 根据key调用阿里云工具类, 删除阿里oss服务器中的文件
        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, key);

        //2. 根据key删除数据库中的数据
        QueryWrapper<DtsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("good_sn", key);
        goodsMapper.delete(queryWrapper);
    }

}

